﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Data.Models.Grassroots;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal.Grassroots
{
    public partial class ItemStatistics : PortalPage
    {
        #region 属性
        /// <summary>
        /// DataSource
        /// </summary>
        public DataSet DataSource
        {
            get
            {
                return null;
            }
        }

        public string ItemID
        {
            get
            {
                return Request.QueryString["ItemID"] ?? "";
            }
        }




        public string StartTime
        {
            get { return string.IsNullOrEmpty(dtStatistics.Text.Trim()) ? "1990-01-01" : dtStatistics.Text.Trim(); }
        }

        public string EndTime
        {
            get { return string.IsNullOrEmpty(dtEndTime.Text.Trim()) ? "2100-01-01" : dtEndTime.Text.Trim(); }
        }

        #endregion

        #region Page_Load
        /// <summary>
        /// Page_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                if (!string.IsNullOrEmpty(this.ItemID))
                {
                    //this.lbGroupName.Text = GrassrootsGroup.GetName(this.GroupID);
                    //this.Title = this.lbGroupName.Text + "-情况统计";
                    this.hdItemID.Value = this.ItemID;
                    //this.spUrl.Attributes["href"] = PageUrlGroup;

                }
                else
                {
                    //this.lbGroupName.Text = "";
                    //this.Title = this.lbGroupName.Text + "情况统计";
                    // this.spLastUrlNode.Visible = false;
                    //this.spUrl.Visible = false;
                    this.hdItemID.Value = this.ItemID;
                }
            }
        }
        #endregion

        #region 数据访问
        /// <summary>
        /// BindData
        /// </summary>
        protected void BindData()
        {
            dtStatistics.Text = DateTime.Now.ToString("yyyy-MM-01");
            dtEndTime.Text = Convert.ToDateTime(dtStatistics.Text).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// LoadData
        /// </summary>
        protected void LoadData()
        {
        }


        #region 单个街道  前台数据
        //单个街道  前台
        public string GetDataString()
        {
            string streetname = "";
            string data = string.Empty;
            string categories = string.Empty;
            int i = 1;

            string fileStr = "'" + this.ItemID + "'";
            DataTable gdt = PartyGrassrootsGroupReport.GetAllGroups(fileStr).Tables[0];
            DataTable sdt = new DataTable();

            if (!string.IsNullOrEmpty(this.ItemID))
            {
                streetname = Convert.ToString(gdt.Rows[0][1]);
            }
            var date = "";
            if (dtStatistics.Text != "")
            {
                date = Convert.ToDateTime(dtStatistics.Text).ToString("yyyy年MM月dd日");
                if (dtStatistics.Text != dtEndTime.Text)
                    date += Convert.ToDateTime(dtEndTime.Text).ToString("--yyyy年MM月dd日");
            }
            data += "<chart caption=\"" + date + streetname + "领导干部驻点普遍直接联系群众工作统计表\" baseFontSize=\"12\"  showvalues=\"0\" areaOverColumns=\"0\" formatNumberScale=\"0\" showSum=\"1\" labelDisplay=\"AUTO\"  showPercentValues=\"0\" showYAxisValues=\"0\">";
            categories += "<categories>";
            categories += "<category label=\"驻点团队数\" />";
            categories += "<category label=\"接待群众（人次）\" />";
            categories += "<category label=\"入户群众（户数）\" />";
            //categories += "<category label=\"走访慰问居民  （户数）\" />";
            //categories += "<category label=\"走访企业、个体工商户、辖内单位（户数）\" />";
            categories += "<category label=\"收集意见建议（条数）\" />";
            categories += "<category label=\"帮助解决的问题（件数）\" />";
            categories += "</categories>";
            data += categories;


            foreach (DataRow dr in gdt.Rows)
            {

                data += "<dataset seriesName=\"" + Convert.ToString(dr["GroupName"]) + "\" color=\"" + GetColor(i) + "\">";

                sdt = PartyGrassrootsGroupReport.GetStatisticsReportData(Convert.ToString(dr["GroupID"]), StartTime, EndTime).Tables[0];


                DataRow sdr = sdt.Rows[0];
                data += string.Format("<set toolText=\"{0}\"  value=\"{0}\" />",
                    string.IsNullOrEmpty(Convert.ToString(dr["ZhudianCount"])) ? "0" : Convert.ToString(dr["ZhudianCount"]));
                data += string.Format("<set toolText=\"{0}\" value=\"{0}\" />",
                    string.IsNullOrEmpty(Convert.ToString(sdr["jiedaiRenci"])) ? "0" : Convert.ToString(sdr["jiedaiRenci"]));
                data += string.Format("<set toolText=\"{0}\" value=\"{0}\" />",
                     string.IsNullOrEmpty(Convert.ToString(sdr["ruhuHuci"])) ? "0" : Convert.ToString(sdr["ruhuHuci"]));
                data += string.Format("<set toolText=\"{0}\" value=\"{0}\" />",
                    string.IsNullOrEmpty(Convert.ToString(sdr["jianyishu"])) ? "0" : Convert.ToString(sdr["jianyishu"]));
                data += string.Format("<set toolText=\"{0}\" value=\"{0}\" />",
                    string.IsNullOrEmpty(Convert.ToString(sdr["jiejueWentishu"])) ? "0" : Convert.ToString(sdr["jiejueWentishu"]));

                data += "</dataset>";
                i++;
            }
            data += "</chart>";

            return data;
        }
        #endregion

        


        protected string GetColor(int i)
        {
            string retStr = string.Empty;
            switch (i)
            {
                case 1: retStr = "9799D1"; break;
                case 2: retStr = "73AC37"; break;
                case 3: retStr = "FFCC00"; break;
                case 4: retStr = "009999"; break;
                case 5: retStr = "CCCC66"; break;
                case 6: retStr = "FFFFCC"; break;
                case 7: retStr = "669900"; break;
                case 8: retStr = "00FFFF"; break;
                case 9: retStr = "996666"; break;
                case 10: retStr = "FF66FF"; break;
                case 11: retStr = "FF9933"; break;
                case 12: retStr = "99CC99"; break;
                case 13: retStr = "999966"; break;
                case 14: retStr = "669933"; break;
                case 15: retStr = "660033"; break;
                case 16: retStr = "000033 "; break;
                case 17: retStr = "CC6633"; break;
                case 18: retStr = "CCCC00"; break;
                case 19: retStr = "FF99FF"; break;
                case 20: retStr = "CC0033 "; break;
                default: retStr = "FF0000"; break;
            }
            return retStr;
        }
        #endregion

        #region 按钮事件
        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.LoadData();
        }

        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            ExportDetails();
        }

        protected void ExportDetails()
        {
            string fileStr = "";
            DataTable gdt = new DataTable();
            if (!string.IsNullOrEmpty(this.ItemID))
            {
                fileStr = "'" + this.ItemID + "'";
                gdt = PartyGrassrootsGroupReport.GetAllGroups(fileStr).Tables[0];
            }
            else
            {
                gdt = PartyGrassrootsGroupReport.GetAllGroups(fileStr).Tables[0];
                DataRow indr = gdt.NewRow();
                indr[0] = "f23afae9fbd94155abca1c20911ae054";
                indr[1] = "山海天旅游度假区";
                indr[2] = PartyGrassrootsGroupItem.GetAllCount();
                gdt.Rows.InsertAt(indr, 0);
            }

            DataTable sdt = new DataTable();

            //string path = Server.MapPath(Request.ApplicationPath) + @"Admin\Project\OrganisationDepartment\Templates\SituationStatistics.xlsx";
            //Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(path);
            //Aspose.Cells.Worksheet sheetDetails = wb.Worksheets[0];
            //sheetDetails.Name = "Sheet1";

            //Aspose.Cells.Cells cells = sheetDetails.Cells;
            //int rowIndex = 0;

            //foreach (DataRow gdr in gdt.Rows)
            //{
            //    //sdt = GrassrootsGroupReport.GetStatisticsReportData(Convert.ToString(gdr["GroupID"]), this.dtStatistics.Text).Tables[0];
            //    if (Convert.ToString(gdr["GroupID"]) == "f23afae9fbd94155abca1c20911ae054")
            //    {
            //        sdt = GrassrootsGroupReportV2.GetAllStatisticsReportData(StartTime, EndTime).Tables[0];
            //    }
            //    else
            //    {
            //        sdt = GrassrootsGroupReportV2.GetStatisticsReportData(Convert.ToString(gdr["GroupID"]), StartTime, EndTime).Tables[0];
            //    }

            //    string GroupName = Convert.ToString(gdr["GroupName"]);

            //    string cell1 = string.IsNullOrEmpty(Convert.ToString(gdr["ZhudianCount"])) ? "0" : Convert.ToString(gdr["ZhudianCount"]);

            //    string cell2 = string.IsNullOrEmpty(Convert.ToString(sdt.Rows[0]["jiedaiRenci"])) ? "0" : Convert.ToString(sdt.Rows[0]["jiedaiRenci"]);
            //    string cell21 = string.IsNullOrEmpty(Convert.ToString(sdt.Rows[0]["ruhuHuci"])) ? "0" : Convert.ToString(sdt.Rows[0]["ruhuHuci"]);
            //    //string cell3 = string.IsNullOrEmpty(Convert.ToString(sdt.Rows[0]["jumingHuci"])) ? "0" : Convert.ToString(sdt.Rows[0]["jumingHuci"]);
            //    //string cell4 = string.IsNullOrEmpty(Convert.ToString(sdt.Rows[0]["qiyeHuci"])) ? "0" : Convert.ToString(sdt.Rows[0]["qiyeHuci"]);
            //    string cell5 = string.IsNullOrEmpty(Convert.ToString(sdt.Rows[0]["jianyishu"])) ? "0" : Convert.ToString(sdt.Rows[0]["jianyishu"]);
            //    string cell6 = string.IsNullOrEmpty(Convert.ToString(sdt.Rows[0]["jiejueWentishu"])) ? "0" : Convert.ToString(sdt.Rows[0]["jiejueWentishu"]);

            //    cells[rowIndex + 1, 0].PutValue(GroupName);
            //    cells[rowIndex + 1, 1].PutValue(cell1);
            //    cells[rowIndex + 1, 2].PutValue(cell2);
            //    cells[rowIndex + 1, 3].PutValue(cell21);

            //    cells[rowIndex + 1, 4].PutValue(cell5);
            //    cells[rowIndex + 1, 5].PutValue(cell6);

            //    rowIndex++;
            //}

            //string docname = "";
            //if (!string.IsNullOrEmpty(this.GroupID))
            //{
            //    docname = "天河区" + gdt.Rows[0][1] + "领导干部驻点普遍直接联系群众工作统计表";
            //}
            //else
            //{
            //    docname = "天河区领导干部驻点普遍直接联系群众工作统计表";
            //}

            //  wb.Save(Server.UrlPathEncode(docname) + "[" + this.dtStatistics.Text + "--" + this.dtEndTime.Text + "]" + ".xlsx",
            //Aspose.Cells.SaveType.OpenInExcel, Aspose.Cells.FileFormatType.Xlsx, Response);

        }
        #endregion

       
    }
}